<?php

namespace IMooc;

class Config implements \ArrayAccess {

    //用来保存文件所在的系统目录
    protected $_path;
    //用来保存已经加载过的配置
    protected $_configs = array();

    //$path,表示文件所在的系统目录
    function __construct($path)
    {
        $this->_path = $path;
    }

    //获取数组的key
    function offsetGet($key)
    {
        if (empty($this->_configs[$key]))
        {
            //生成文件的绝对路径
            $file_path = $this->_path . '/' . $key . '.php';
            $config = require $file_path;
            $this->_configs[$key] = $config;
        }
        return $this->_configs[$key];
    }

    //设置数组的key
    function offsetSet($key, $value)
    {
        throw new \Exception('cannot write config file.');
    }

    //检测数组key是否存在
    function offsetExists($key)
    {
        return isset($this->_configs[$key]);
    }

    //删除数组的key
    function offsetUnset($key)
    {
        
    }

}
